new Vue({
    el: '#app',
    data: {
        id: '',
        prizeId: '',
        msg: '',
        member: {},
        formData: {},
        SERVER_HOST: SERVER_HOST
    },
    mounted() {
        var member = Cookies.get('member');
        if (!member || member == null || member == '') {
            window.location.href = 'dl.html';
        } else {
            this.member = JSON.parse(member);
        }
        this.prizeId = getUrlKey('prizeId');
        this.id = getUrlKey('id');
        this.load();
    },
    methods: {
        exit () {
            Cookies.remove('member');
            window.location.href = 'dl.html';
        },
        load () {
            if (this.id) {
                getAjax().post('/web/appealForm', toFormData({'id': this.id})).then(res => {
                    this.formData = res.data.data;
                });
            }
        },
        save () {
            if (!this.formData.remark) {
                alert('请填写申诉原因！');
                return;
            }
            var file = document.getElementById('file').files[0];
            if (file) {
                getAjax().post('/upload/uploadFile', toFormData({'file': file})).then(res => {
                    let data = res.data.data;
                    if (data) {
                        this.appealSave(data.imgUrl);
                    } else {
                        alert('文件上传失败！');
                    }
                });
            } else {
                this.appealSave('');
            }
        },
        appealSave (filePath) {
            let formData = {};
            formData['remark'] = this.formData.remark;
            formData['prizeId'] = this.prizeId;
            formData['appealBy'] = this.member['id'];
            formData['upload'] = filePath;
            getAjax().post('/web/appealSave', toFormData(formData)).then(res => {
                if (res.data.code === 200) {
                    alert('已提交申请！');
                    window.location.reload();
                } else {
                    alert('申请失败！');
                }
            });
        },
        back (href) {
            window.location.href = href;
        }
    }
});
